﻿
using GCC.APIContainer.Utilities;
using GCC.Container.Abstracts;
using GCC.Container.Models;

using GCCGrpc.FileServer;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Z.AllInOneDemo
{
    [ApiController]
    [Route("allInOne/[controller]")]
    public class DemoController : GCCAPIControllerBase
    {
        public DemoController(AbsGCCContext context) : base(context)
        {
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost("setFile")]
        public DataResponse<GRPCCreateBigFileResponse> SetFile(IFormFile file)
        {
            GCCContext.SaveTrace("custome", "诶~我存了一下,", path: "自己定义的内容", method: "想写什么写什么");
            if (file is null)
            {
                return InvalidParameter<GRPCCreateBigFileResponse>();
            }
            var fileServerHost = GCCContext.GetConfigByPath(new string[] { GCCContext.PointName ?? "", "FileServer" });
            var fileFolderPath = Path.Combine(AppContext.BaseDirectory, Guid.NewGuid().ToString("N"));
            var filePath = Path.Combine(fileFolderPath, file.FileName);
            _ = Directory.CreateDirectory(fileFolderPath);
            {
                using var fs = System.IO.File.Open(filePath, FileMode.CreateNew, FileAccess.ReadWrite);
                file.CopyTo(fs);
                fs.Flush();
                fs.Close();
            }
            GCCContext.SaveTrace("custome", "诶~我又存了一下,");
            GRPCCreateBigFileResponse? res = null;
            foreach (var item in System.FileServer.Client.Manager.GRPCCreateBigFile(fileServerHost, filePath, GCCContext.EventID ?? ""))
            {
                res = item.res;
            }


            if (res is null)
            {
                GCCContext.SaveTrace("custome", "诶~错误返回,");
                return InvalidParameter<GRPCCreateBigFileResponse>();
            }
            GCCContext.SaveTrace("custome", "诶~正确返回,");
            return HandleData(res);
        }

        /// <summary>
        /// 保存缓存
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost("saveToCache")]
        public async Task<DataResponse<bool>> saveToCache([FromBody] Dictionary<string, string> data)
        {
            await GCCContext.Set<Dictionary<string, string>>("myCache", data, new CacheItemSettings()
            {
                SlidingExpiration = new TimeSpan(0, 0, 60)
            });
            return HandleData(true);
        }

        /// <summary>
        /// 保存缓存
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpGet("getCache")]
        public async Task<DataResponse<Dictionary<string, string>>> getCache()
        {
            var res = await GCCContext.TryGet<Dictionary<string, string>>("myCache");

            return HandleData(res);
        }

        /// <summary>
        /// 保存到MQ
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost("saveToMQ")]
        public DataResponse<bool> SaveToMQ([FromBody] Dictionary<string, string> data)
        {
            var res = GCCContext.SendMessageToLocalMQ(System.Text.Json.JsonSerializer.Serialize(data), "test");
            return HandleData(res);
        }
        /// <summary>
        /// 读取MQ
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpGet("readMQ")]
        public DataResponse<Dictionary<string, string>> GetFromMQ()
        {
            Dictionary<string, string> res = new Dictionary<string, string>();
            var w = new AutoResetEvent(false);
            GCCContext.GotOneMessageFromLocalMQ((data) =>
            {
                _ = w.Set();
                var temp = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, string>>(data);
                if (temp != null)
                {
                    res = temp;
                }
                return true;
            }, "test");
            return HandleData(res);
        }

    }
}
